package com.dragon.datastructure.linkedlist;

/**
 * https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
 * <p>
 * 编写一个程序，找到两个单链表相交的起始节点。
 *
 * @author dragon
 * @since 2019/12/20
 */
public class Test11 {


    private static class ListNode {
        int val;
        ListNode next;
    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pA = headA;
        ListNode pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }

        return pA;
    }
}
